from extensions import db
from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash

# 用户模型
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    role = db.Column(db.String(20), nullable=False, default='employee')  # 角色：admin 或 employee
    name = db.Column(db.String(100), nullable=True)  # 用户姓名
    gender = db.Column(db.String(10))  # 性别：male 或 female
    birth_date = db.Column(db.Date)  # 出生日期
    phone = db.Column(db.String(20))  # 联系电话
    email = db.Column(db.String(100), unique=True)  # 邮箱
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'), nullable=True)  # 部门ID
    position = db.Column(db.String(100))  # 职位
    address = db.Column(db.Text)  # 地址
    avatar = db.Column(db.String(255))  # 头像地址
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 与部门的关联
    department = db.relationship('Department', backref='users', lazy=True)
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
        
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

# 部门模型
class Department(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=True, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

# 员工模型
class Employee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)  # 关联用户ID
    name = db.Column(db.String(100), nullable=False)
    gender = db.Column(db.String(10))  # 性别：male 或 female
    phone = db.Column(db.String(20))
    position = db.Column(db.String(100))
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'), nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 与用户的关联
    user = db.relationship('User', backref='employee', lazy=True)
    # 与部门的关联
    department = db.relationship('Department', backref='employees', lazy=True)

# 考勤模型
class Attendance(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'), nullable=False)
    date = db.Column(db.Date, nullable=False)
    check_in_time = db.Column(db.Time)
    check_out_time = db.Column(db.Time)
    status = db.Column(db.String(20), default='present')  # 考勤状态：present、absent、late、early_leave
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

# 绩效评估模型
class Performance(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    employee_id = db.Column(db.Integer, db.ForeignKey('employee.id'), nullable=False)
    period = db.Column(db.String(20), nullable=False)  # 评估周期，如：2023-Q1、2023-01等
    score = db.Column(db.Float, nullable=False)
    comments = db.Column(db.Text)
    evaluator_id = db.Column(db.Integer, db.ForeignKey('employee.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    evaluator = db.relationship('Employee', foreign_keys=[evaluator_id], backref='evaluations', lazy=True)
    employee = db.relationship('Employee', foreign_keys=[employee_id], backref='performance_records', lazy=True)